<?php

class Member_Model extends CI_Model {

    function validate() {
        $this->db->where('USERNAME', $this->input->post('username'));
        $this->db->where('PASSWORD', md5($this->input->post('password')));
        $query = $this->db->get('MEMBER');

        //if($query->num_rows == 1)
        //{
        //	return true;
        //}

        return $query;
    }

    function searchMember($firstname, $lastname, $citizenid, $per_page, $offset) {
        $this->db->from('MEMBER');

        if ($firstname) {
            $this->db->like('FIRSTNAME', $firstname);
        }
        if ($lastname) {
            $this->db->like('LASTNAME', $lastname);
        }

        if ($citizenid) {
            $this->db->where('ID_CARD', $citizenid);
        }

//        $this->db->where('APPROVE_FLAG', $status);

        $this->db->where('USER_TYPE', 2);
        
        $this->db->limit($per_page, $offset);
        
        $query = $this->db->get();

        return $query;
    }

    function addMember($member, $memberMenuList) {
        $this->db->trans_start();
        $this->db->insert('MEMBER', $member);
        $memberID = $this->db->insert_id();

        //insert menu permission 1 = Home, 7 = Select Course
        foreach ($memberMenuList as $memberMenu) {

            $data = array(
                'MEMBER_ID' => $memberID,
                'MENU_ID' => $memberMenu);

            $this->db->insert('menu_permission', $data);
        }
        
        if ($this->db->trans_status() === FALSE) {
            $this->db->trans_rollback();
        } else {
            $this->db->trans_commit();
        }
    }

    function updateMember($member, $citizenId) {
        if (!$citizenId) {
            $this->db->where('ID', $this->session->userdata('memberID'));
        } else {
            $this->db->where('ID', $citizenId);
        }
        $this->db->update('MEMBER', $member);
    }

    function getAllMember($per_page, $offset) {
//        $this->db->select('ID, FIRSTNAME, LASTNAME, ID_CARD, APPROVE_FLAG');
        $this->db->select('ID, FIRSTNAME, LASTNAME, ID_CARD');
        $this->db->from('MEMBER');
        $this->db->where('USER_TYPE', 2);
        $this->db->limit($per_page, $offset);
        $query = $this->db->get();

        return $query;
    }

    function getMemberByID($memberID) {
        $this->db->from('MEMBER');
        $this->db->where('ID', $memberID);
        $query = $this->db->get();

        return $query;
    }
    function getMemberByUsername($username) {
        $this->db->from('MEMBER');
        $this->db->where('USERNAME', $username);
        $query = $this->db->get();

        return $query;
    }

    function getMemberByUserAndCitizen($memberUser, $citizenId) {
        $where = 'USERNAME = "' . $memberUser . '"and ID_CARD ="' . $citizenId . '"';
        $this->db->from('MEMBER');
        $this->db->where($where, NULL, false);
        $query = $this->db->get();

        return $query;
    }

    function approveMember($memberID, $memberObj, $menuIDList) {


        $this->db->trans_begin();


        //update approve status
        $this->db->where('ID', $memberID);
        $this->db->update('MEMBER', $memberObj);


        //insert menu permission
        foreach ($menuIDList as $menuID) {
//            echo $menuID->ID;
            $data = array(
                'MEMBER_ID' => $memberID,
                'MENU_ID' => $menuID);

            $this->db->insert('menu_permission', $data);
        }

        //insert video permission


        if ($this->db->trans_status() === FALSE) {
            $this->db->trans_rollback();
        } else {
            $this->db->trans_commit();
        }
    }

//    function updateMemberTimes($memberID, $times) {
//        $this->db->trans_begin();
//
//        $data = array(
//            'TIMES' => $times,
//        );
//
//        $this->db->where('ID', $memberID);
//        $this->db->update('MEMBER', $data);
//
//        if ($this->db->trans_status() === FALSE) {
//            $this->db->trans_rollback();
//        } else {
//            $this->db->trans_commit();
//        }
//    }

    function getMemberList() {
        $this->db->from('MEMBER');
        $this->db->where('USER_TYPE','2');
        $this->db->order_by('ID');
        $result = $this->db->get();
        $return = array();
        
        if ($result->num_rows() > 0) {
            foreach ($result->result_array() as $row) {
                $return[$row['ID']] = $row['USERNAME'];
            }
        }

        return $return;
    }

    function getMemberSelectList() {
        $this->db->from('MEMBER');
        $this->db->order_by('ID');
        $result = $this->db->get();

        return $result->result();
    }

}

?>
